//
//  MenuCollectionController.swift
//  lookLikeIweekly
//
//  Created by Mainzxq on 16/6/12.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import UIKit

// 协议传值
protocol sendDataProtocl: NSObjectProtocol {
    func tapBarBtn(barIndex: Int)
}

private let reuseIdentifier = "menucells"

class MenuCollectionController: UICollectionViewController,UICollectionViewDelegateFlowLayout {
    typealias sendDataClosure=(number:Int) -> Void
    private var menuArray = [String]()
    var indexNum: Int = 0
    var myClosure: sendDataClosure?
    var delegate: sendDataProtocl?
    func callBack(call:sendDataClosure) {
        myClosure = call
    }
    
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        super.init(collectionViewLayout: LineLayOut(count:menuArray.count))
        self.collectionView = NewsMenuCollectionView.init(frame: CGRectMake(0, 0, 50*CGFloat(menuArray.count), 25), collectionViewLayout: LineLayOut(count:menuArray.count))
        for item in titleArray {self.menuArray.append(item.title)}
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupFrame()

        // Register cell classes
        self.collectionView!.registerClass(NewsMenuCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        print("cell done")
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return menuArray.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewsMenuCell
        cell.menuLbl.text = menuArray[indexPath.row]
        return cell
    }
    
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.indexNum = indexPath.row
        tapped()
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    }
    
    
    override func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
        
    }

    
    func setupFrame() {
        let myLayOut = LineLayOut(count:menuArray.count)
        self.view.backgroundColor = UIColor.whiteColor()
        self.collectionView?.showsVerticalScrollIndicator=false
        self.collectionView?.showsHorizontalScrollIndicator=false
        self.collectionView?.collectionViewLayout = myLayOut
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        self.collectionView?.frame.origin = CGPointMake(0, 0)
        self.collectionView?.frame.size = CGSizeMake(self.view.frame.size.width, 25)
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
        self.collectionView?.bounces = false
    }
    
    func tapBarBtn(barIndex: Int) {
        print(barIndex)
        self.delegate?.tapBarBtn(barIndex)
    }
    
    func tapped() {
        print(self.indexNum)
        self.myClosure!(number: self.indexNum)
        self.delegate?.tapBarBtn(self.indexNum)
    }

}
